<template>
  <div class="app-container">
    <!-- 开关日志 -->
    <!-- 查询和其他操作 -->

    <el-form :inline="true" class="filter-container filter">
      <div>
        <el-form-item label="日志类型">
          <el-select v-model="listQuery.ftype" clearable filterable class="filter-item" placeholder="请选择日志类型">
          <el-option v-for="(key, value) in userTypeMap" :key="key" :label="key" :value="value"/>
        </el-select>
        </el-form-item>
        <el-form-item label="MAC">
          <el-input v-model="listQuery.fmacno" clearable class="filter-item" style="width: 200px;" placeholder="请输入mac"/>
        </el-form-item>
        <el-form-item label="动作">
          <el-input v-model="listQuery.faction" clearable class="filter-item" style="width: 200px;" placeholder="请输入动作"/>
        </el-form-item>
        <el-form-item label="场景编号">
          <el-input v-model.number="listQuery.fsceneno" clearable class="filter-item" style="width: 200px;" placeholder="请输入场景编号"/>
        </el-form-item>
      </div>
      <el-form-item label="操作员编号">
      <el-input v-model="listQuery.foperator" clearable class="filter-item" style="width: 200px;" placeholder="请输入操作员编号"/>
      </el-form-item>
      <el-form-item label="日期">
      <el-date-picker v-model="listQuery.fcreatetime" value-format="yyyy/MM/dd" type="date" placeholder="请选择日期"/>
      </el-form-item>
      <el-form-item>
        <el-button round size="medium" class="filter-item"
          :style="{ background: buttonColor.lookup, color: buttonColor.color }" @click="handleFilter">查找</el-button>
      </el-form-item>
    </el-form>

    <!-- 查询结果 -->
    <el-table :height="buttonColor.clientHeightTwo" v-loading="listLoading" :data="list" element-loading-text="正在查询中。。。"
      border fit highlight-current-row :header-cell-style="tableColor">
      <el-table-column :index="indexMethod" align="center" label="序" type="index" />
      <!--<el-table-column align="right" label="类型" prop="ftype" />-->
      <el-table-column align="center" label="类型" prop="ftype">
        <template slot-scope="scope">
          <el-tag>{{ scope.row.ftype | userTypeFilter }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column align="right" label="smart的mac" prop="fmacno" />
      <el-table-column align="right" label="动作" prop="faction" />
      <el-table-column align="right" label="指令" prop="finstructions" />
      <el-table-column align="right" label="场景编号" prop="fsceneno" />
      <el-table-column align="right" label="操作员" prop="foperator" />
      <el-table-column :formatter="datetimeFormat" align="right" label="时间" prop="fcreatetime" />
      <el-table-column align="right" label="实验室编号" prop="fpointNo" />
      <!--<el-table-column align="right" label="方法名" prop="methodName" />-->
      <el-table-column align="center" label="操作员" prop="foperator" />
      <!--<el-table-column align="center" label="操作" class-name="small-padding">
        <template slot-scope="scope">
          <el-button type="primary" size="mini" class="button-margin" @click="handleUpdate(scope.row)">编辑</el-button>&lt;!&ndash;v-permission="[':sLog:update']" &ndash;&gt;
          <el-button type="danger" size="mini" class="button-margin" @click="handleDelete(scope.row)">删除</el-button>&lt;!&ndash;v-permission="[':sLog:delete']" &ndash;&gt;
        </template>
      </el-table-column>-->
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />

    <!-- 添加或修改对话框 -->
    <el-dialog :close-on-click-modal="false" :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible">
      <el-form ref="dataForm" :rules="rules" :model="dataForm" status-icon label-position="left" label-width="100px" style="width: 400px; margin-left:50px;">
        <el-form-item label="类型" prop="ftype">
          <el-input v-model.number="dataForm.ftype"/>
        </el-form-item>
        <el-form-item label="smart的mac" prop="fmacno">
          <el-input v-model="dataForm.fmacno"/>
        </el-form-item>
        <el-form-item label="动作" prop="faction">
          <el-input v-model="dataForm.faction"/>
        </el-form-item>
        <el-form-item label="指令" prop="finstructions">
          <el-input v-model="dataForm.finstructions"/>
        </el-form-item>
        <el-form-item label="场景编号" prop="fsceneno">
          <el-input v-model.number="dataForm.fsceneno"/>
        </el-form-item>
        <el-form-item label="操作员" prop="foperator">
          <el-input v-model="dataForm.foperator"/>
        </el-form-item>
        <el-form-item label="创建时间" prop="fcreatetime">
          <el-date-picker v-model="dataForm.fcreatetime" type="datetime"/>
        </el-form-item>
        <el-form-item label="实验室编号" prop="fpointNo">
          <el-input v-model="dataForm.fpointNo"/>
        </el-form-item>
        <el-form-item label="方法名" prop="methodName">
          <el-input v-model="dataForm.methodName"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取消</el-button>
        <el-button v-if="dialogStatus=='create'" type="primary" @click="createData">确定</el-button>
        <el-button v-else type="primary" @click="updateData">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<style>
  .button-margin {
    margin : 2px 0px;
  }
  .el-button+.el-button {
    margin-left: 1px;
  }
</style>

<script>
import { listsLog, createsLog, updatesLog, deletesLog } from '@/api/sLog'
import Pagination from '@/components/Pagination'
import { tableColor, titleColor, buttonColor } from '@/filters/index'
import moment from 'moment'
const userTypeMap = {
  1: '定时场景',
  2: '触发场景',
  3: '手动场景',
  4: '手机面板指令',
  5: '手机测试指令'
}
export default {
  name: 'SLog',
  components: { Pagination },
  filters: {
    userTypeFilter(type) {
      return userTypeMap[type]
    }
  },
  data() {
    return {
      tableColor, // 表格颜色
      titleColor, // 弹窗颜色
      buttonColor, // 按钮颜色
      list: null,
      total: 0,
      listLoading: true,
      userTypeMap,
      listQuery: {
        page: 1,
        limit: 20,
        ftype: undefined,
        fmacno: undefined,
        faction: undefined,
        finstructions: undefined,
        fsceneno: undefined,
        foperator: undefined,
        fcreatetime: undefined,
        fpointNo: undefined,
        methodName: undefined,
        sort: 'create_time',
        order: 'desc'
      },
      dataForm: {
        ftype: undefined,
        fmacno: undefined,
        faction: undefined,
        finstructions: undefined,
        fsceneno: undefined,
        foperator: undefined,
        fcreatetime: undefined,
        fpointNo: undefined,
        methodName: undefined,
        id: undefined
      },
      dialogFormVisible: false,
      dialogStatus: '',
      textMap: {
        update: '编辑',
        create: '创建'
      },
      rules: {
        ftype: [

          { type: 'number', message: '类型必须为数字值' }

        ],
        fmacno: [

        ],
        faction: [

        ],
        finstructions: [

        ],
        fsceneno: [

          { type: 'number', message: '场景编号必须为数字值' }

        ],
        foperator: [

        ],
        fcreatetime: [

        ],
        fpointNo: [

        ],
        methodName: [

        ]
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    datetimeFormat(row, column) {
      const date = row[column.property]
      if (date === undefined || date === null) {
        return ''
      }
      return moment(date).format('YYYY-MM-DD HH:mm:ss')
    },
    getList() {
      this.listLoading = true
      listsLog(this.listQuery)
        .then(response => {
          this.list = response.data.data.list
          this.total = response.data.data.total
          this.listLoading = false
        })
        .catch(() => {
          this.list = []
          this.total = 0
          this.listLoading = false
        })
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    resetForm() {
      this.dataForm = {
        ftype: undefined,
        fmacno: undefined,
        faction: undefined,
        finstructions: undefined,
        fsceneno: undefined,
        foperator: undefined,
        fcreatetime: undefined,
        fpointNo: undefined,
        methodName: undefined,
        id: undefined
      }
    },
    handleCreate() {
      this.resetForm()
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    createData() {
      this.$refs['dataForm'].validate(valid => {
        if (valid) {
          createsLog(this.dataForm)
            .then(response => {
              this.list.unshift(response.data.data)
              this.dialogFormVisible = false
              this.$notify.success({
                title: '成功',
                message: '添加SLog成功'
              })
            })
            .catch(response => {
              this.$notify.error({
                title: '失败',
                message: response
              })
            })
        }
      })
    },
    handleUpdate(row) {
      this.dataForm = Object.assign({}, row)
      this.dialogStatus = 'update'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    updateData() {
      this.$refs['dataForm'].validate(valid => {
        if (valid) {
          updatesLog(this.dataForm, this.$route.query.id)
            .then(() => {
              for (const v of this.list) {
                if (v.id === this.dataForm.id) {
                  const index = this.list.indexOf(v)
                  this.list.splice(index, 1, this.dataForm)
                  break
                }
              }
              this.dialogFormVisible = false
              this.$notify.success({
                title: '成功',
                message: '更新SLog成功'
              })
            })
            .catch(response => {
              this.$notify.error({
                title: '失败',
                message: response
              })
            })
        }
      })
    },
    handleDelete(row) {
      this.$confirm('是否要删除SLog', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deletesLog(row)
          .then(response => {
            this.$notify.success({
              title: '成功',
              message: '删除SLog成功'
            })
            const index = this.list.indexOf(row)
            this.list.splice(index, 1)
          })
          .catch(response => {
            this.$notify.error({
              title: '失败',
              message: response
            })
          })
      })
    },
    indexMethod(index) {
      index = index + 1
      return index
    }
  }
}
</script>
